Iterate over alle modules in the current folder?

Hi!

I will iterate with a for-loop over all modules in the current folder in that way, that I can assign this current module (which is in every iteration of the for-loop a another module) to a function which expects modules.

 

I call with this code my function:

Folder f = current

iterateExportModuleToExcel(f)

 

The function goes like that:

void iterateExportModuleToExcel(Folder f)
{
    //Implements a loop over all modules, which will be
    //found in the current path of the Doors explorer.
    
    //Debug
    print "Function ""iterateExportModuleToExcel"" reached. \n"
    
    Module m
    int i = 0
    for m in f do
    {
        //Debug
        //print name(m) "\n"
        //Debug
        i+=1
        print "Test " i "\n"
        
        //Call every iteration the function "exportModuleToExcel"        
    }
}

 

 

The Problem now is, that the for loop obviously don't iterate over the modules, because I don't get the print statement within the for-loop.

What is false in my function?


tobi_mehrl - Mon Apr 27 06:35:16 EDT 2015

Re: Iterate over alle modules in the current folder?
doors36677 - Mon Apr 27 07:18:32 EDT 2015

Try something like:

Item i 
for i in current Folder do 
{
    print (name i) "\n"

}

Re: Iterate over alle modules in the current folder?
tobi_mehrl - Mon Apr 27 14:45:05 EDT 2015

doors36677 - Mon Apr 27 07:18:32 EDT 2015

Try something like:

Item i 
for i in current Folder do 
{
    print (name i) "\n"

}

Thank you. This suggestion is running now.

But now, can you say me, that I come from Item i (which has obviously the information from the module...) to a module m?

Is there any cast from Item to Module?

 

If I declare a variable Module m and assign: m = module(i) in the for-loop and if i run the code, I don't get any warnings. But if I write additionally print name(m) I get any warnings. The function now is looking like that:

 

void iterateExportModuleToExcel()
{
    Item i
    Module m
    
    for i in current Folder do
    {

        //Can I write some type of type-cast? How can I get Module m depending on Item i?
        m = module(i)
        print name(m)     //There is any problem...
    }
}

 

I need a module in each iteration becaus I have to call a function with parameter Module in each iteration...

 

Can you help me?

 

Re: Iterate over alle modules in the current folder?
doors36677 - Mon Apr 27 15:00:15 EDT 2015

tobi_mehrl - Mon Apr 27 14:45:05 EDT 2015

Thank you. This suggestion is running now.

But now, can you say me, that I come from Item i (which has obviously the information from the module...) to a module m?

Is there any cast from Item to Module?

 

If I declare a variable Module m and assign: m = module(i) in the for-loop and if i run the code, I don't get any warnings. But if I write additionally print name(m) I get any warnings. The function now is looking like that:

 

void iterateExportModuleToExcel()
{
    Item i
    Module m
    
    for i in current Folder do
    {

        //Can I write some type of type-cast? How can I get Module m depending on Item i?
        m = module(i)
        print name(m)     //There is any problem...
    }
}

 

I need a module in each iteration becaus I have to call a function with parameter Module in each iteration...

 

Can you help me?

 

Go back to code I gave -- name I is name of module I. e., print (name i) "\n"
 

It is all in the help file for dxl

 

 

 

Re: Iterate over alle modules in the current folder?
Wolfgang Uhr - Tue Apr 28 02:35:42 EDT 2015

tobi_mehrl - Mon Apr 27 14:45:05 EDT 2015

Thank you. This suggestion is running now.

But now, can you say me, that I come from Item i (which has obviously the information from the module...) to a module m?

Is there any cast from Item to Module?

 

If I declare a variable Module m and assign: m = module(i) in the for-loop and if i run the code, I don't get any warnings. But if I write additionally print name(m) I get any warnings. The function now is looking like that:

 

void iterateExportModuleToExcel()
{
    Item i
    Module m
    
    for i in current Folder do
    {

        //Can I write some type of type-cast? How can I get Module m depending on Item i?
        m = module(i)
        print name(m)     //There is any problem...
    }
}

 

I need a module in each iteration becaus I have to call a function with parameter Module in each iteration...

 

Can you help me?

 

Hi Toby

You can use a recursive algorithm to step through all items of your Database

void checkDatabaseItem(Item itmWork) {

    if (type(itmWork) == "Project") {
  ++iCountProjects;
  Item itmHelp;
  for itmHelp in folder(itmWork) do {
   checkDatabaseItem(itmHelp);
  }
 }

 if (type(itmWork) == "Folder") {
  ++iCountFolder;
  Item itmHelp;
  for itmHelp in folder(itmWork) do {
   checkDatabaseItem(itmHelp);
  }
 }

 if (type(itmWork) == "Formal") {
  ModName_ mdlWork = module(fullName(itmWork));
  if (isDeleted(mdlWork)) {
   ++iCountModulesDeleted;
  } else {
   ++iCountModules;
   // Do something with your module
  }
 }

} // checkDatabaseItem

Best regards

Wolfgang

Re: Iterate over alle modules in the current folder?
sekrbo - Tue Apr 28 08:21:37 EDT 2015

tobi_mehrl - Mon Apr 27 14:45:05 EDT 2015

Thank you. This suggestion is running now.

But now, can you say me, that I come from Item i (which has obviously the information from the module...) to a module m?

Is there any cast from Item to Module?

 

If I declare a variable Module m and assign: m = module(i) in the for-loop and if i run the code, I don't get any warnings. But if I write additionally print name(m) I get any warnings. The function now is looking like that:

 

void iterateExportModuleToExcel()
{
    Item i
    Module m
    
    for i in current Folder do
    {

        //Can I write some type of type-cast? How can I get Module m depending on Item i?
        m = module(i)
        print name(m)     //There is any problem...
    }
}

 

I need a module in each iteration becaus I have to call a function with parameter Module in each iteration...

 

Can you help me?

 

Module module(Item itemRef)

Returns a handle of type Module for itemRef if itemRef is an open module. Otherwise, it returns null.

However, an Item can be of different types, so you should first check that the item is a module, if it is not open you have to open the module to get a handle of type Module. As you have the name of the Item that is easily done using the function 'read' for example.

Item i
Module m
bool bOpened // or use a skip list ...

for i in current Folder do
{
    if (type(i) != "Formal") then continue
    bOpened = false
    m = module (i)
    if (null (m)) then {
// open the module in read-only, not displayed, standard view
        m = read (fullName (i), false, true)
        bOpened = true
    }
    print (name (m)) "\n" // or call your function(s)
   
    if (bOpened) then close (m)
}

 /sekrbo

Re: Iterate over alle modules in the current folder?
woodpryan - Tue Apr 28 18:26:59 EDT 2015

This function will recursively loop through a Folder to find all the Modules contained within. If Modules should only be found in the folder and not the folders within the current folder, simply remove the recursive call to loopThroughFolder.

 

void loopThroughFolder(Folder f)
{
    Module mod = null;
    Item i;
    
    if(null == f)
        return;
        
    for i in f do
    {
        if(type(i) == "Folder")
            loopThroughFolder(folder(i));
        else if(type(i) == "Formal")
        {
            mod = read(fullName(i), false);
            print(name(mod) "\n");
            //do whatever else with mod.
        }
    }
}

 

Re: Iterate over alle modules in the current folder?
tobi_mehrl - Mon May 04 05:09:26 EDT 2015

Thanks a lot for your helpfully answers!

However, I have addtionally problems, with my code. I start a own-made function with name "exportModuleToExcel", which I start during if you click a Button in a DialogBox. The code goes like that:

exportButtonDBE = apply( excelExportDB, exportButtonLabel, exportModuleToExcel )

My requirement is, to start the function "exportModuleToExcel" several times. Several times means: Start this function as often as a for-loop (e.g.) finds modules whicht content must be exportet to excel.

Is there any possibility to code this behaviour?